package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.system.PermOption;
import org.example.entity.system.Permission;
import org.example.mapper.PermissionMapper;
import org.example.service.PermissionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 魏锴
 * @description 针对表【tb_sys_perm】的数据库操作Service实现
 * @createDate 2024-01-11 15:16:46
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {

    @Override
    public boolean insertPermission(Permission permission) {
        return baseMapper.insertPermission(permission);
    }
    @Override
    public boolean updatePermission(Permission permission) {return baseMapper.updatePermission(permission);}
    @Override
    public boolean deletePermission(int id) {
        return baseMapper.deletePermission(id);
    }
    public List<Permission> getPermissionList() {
        return baseMapper.getPermissionList();
    }
    @Override
    public List<Integer> getPermsByRoles(List<Integer> role_ids) {
        if(role_ids.isEmpty()) return new ArrayList<>();
        return baseMapper.getPermsByRoles(role_ids);
    }
    @Override
    public List<PermOption> getPermOpsByRoleId(int role_id) {
        List<Permission> permissions = baseMapper.getPermOpsByRoleId(role_id);
        List<PermOption> options = new ArrayList<>();
        for (Permission p : permissions) options.add(new PermOption(p));
        return options;
    }
    @Override
    public Map<String, String> getPermissionIdMap() {
        List<Map<String,Object>> data = baseMapper.getPermissionIdMap();
        Map<String,String> result = new HashMap<>();
        for (Map<String,Object> map:data)
            result.put(String.valueOf(map.get("id")), (String) map.get("title"));
        return result;
    }
    @Override
    public boolean grantPermission(int role_id, int perm_id) {
        return baseMapper.grantPermission(role_id, perm_id);
    }
    @Override
    public boolean revokePermission(int role_id, int perm_id) {
        return baseMapper.revokePermission(role_id, perm_id);
    }
}




